package com.woniu.petkonwledge.controller;

import com.woniu.common.entity.ExposureInfo;
import com.woniu.common.entity.ResponseResult;
import com.woniu.common.entity.User;
import com.woniu.petkonwledge.service.ExposureInfoService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

@RestController
@RequestMapping("exposureInfo")
public class ExposureInfoController {
    @Autowired
    ExposureInfoService exposureInfoService;
    //添加曝光信息
    @ResponseBody
    @PostMapping("/addExposureInfo")
    public ResponseResult<String> addExposureInfo(@Param("describes") String describes,@Param("address") String address,@Param("photo")String photo){
        ResponseResult<String> result = new ResponseResult<>();
        ExposureInfo exposureInfo = new ExposureInfo();
        exposureInfo.setAddress(address);//地址
        exposureInfo.setDescribes(describes);//描述内容
        exposureInfo.setPhoto(photo);//照片路径
        //上传时间，精确到日
        DateFormat df3 = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.CHINA);
        String date1 = df3.format(new Date());

        exposureInfo.setTime(date1);
        User user = new User();
        user.setId(1);
        exposureInfo.setUser(user);
        exposureInfo.setState(0);
        try {
            exposureInfoService.addExposureInfo(exposureInfo);
        }catch (Exception e){
            result.setData(null);
            result.setCode(404);
            result.setMessage("添加失败");
            return result;
        }
        result.setCode(200);
        result.setMessage("添加成功");
        result.setData(null);
        return result;
    }
    //查询曝光消息
    @ResponseBody
    @GetMapping("/selectExposureInfo")
    public ResponseResult<List<ExposureInfo>> selectExposureInfoByState(int state){
        ResponseResult<List<ExposureInfo>> result = new ResponseResult<>();
        List<ExposureInfo> exposureInfos = exposureInfoService.selectExposureInfo(state);
       //成功返回结果
        result.setCode(200);
        result.setMessage("查询成功");
        result.setData(exposureInfos);
        return result;
    }
    //审核曝光内容，外调
    @ResponseBody
    @RequestMapping("/reviewExposureInfo")
    public ResponseResult<String> reviewExposureInfo(@Param("state") int state,@Param("id") int id){
        ResponseResult<String> result = new ResponseResult<>();
        exposureInfoService.reviewExposureInfo(state,id);
        result.setCode(200);
        result.setMessage("操作成功");
        result.setData(null);
        return result;
    }
}
